package com.mokee.android.Login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mokee.android.SharedPreference.MyValuesString;
import com.mokee.android.ShipPass.MainActivity;
import com.mokee.android.ShipPass.R;
import com.mokee.android.ShipPass.WelcomeActivity;

public class LoginActivity extends Activity implements OnClickListener {
	private EditText et_LoginUserName, et_LoginPassward;
	private Button btn_Login;
	private TextView tv_Register;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
	}

	private void initView() {
		et_LoginUserName = (EditText) findViewById(R.id.et_LoginUserName);
		et_LoginPassward = (EditText) findViewById(R.id.et_LoginPassward);

		btn_Login = (Button) findViewById(R.id.btn_Login);
		tv_Register = (TextView) findViewById(R.id.tv_Register);
		
		et_LoginUserName.setText(MyValuesString.getLoginName());
		et_LoginPassward.setText(MyValuesString.getLoginPassward());
	}

	private void initEvent() {
		btn_Login.setOnClickListener(this);
		tv_Register.setOnClickListener(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		initView();
		initEvent();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_Login:
			String login_Name = et_LoginUserName.getText().toString().trim();
			String login_Passward = et_LoginPassward.getText().toString().trim();
			if(login_Name != null && !login_Name.equals("")){
				if(login_Passward != null && !login_Passward.equals("")){
					// 请求登陆
					if(login_Name.equals("admin") && login_Passward.startsWith("888")){
						//记录密码
						MyValuesString.setLoginName(login_Name);
						MyValuesString.setLoginPassward(login_Passward);
						
						//跳转
						Intent main = new Intent(LoginActivity.this, MainActivity.class);
						startActivity(main);
						
						finish();
						onDestroy();
					} else if(login_Name.equals(MyValuesString.getLoginName()) && login_Passward.equals(MyValuesString.getLoginPassward())){
						//跳转
						Intent main = new Intent(LoginActivity.this, MainActivity.class);
						startActivity(main);
						
						finish();
						onDestroy();
					} else {
						Toast.makeText(this, R.string.toast_login_username_passward_error, Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(this, R.string.toast_login_passward, Toast.LENGTH_SHORT).show();
				}
			} else {
				Toast.makeText(this, R.string.toast_login_username, Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.tv_Register:
			// 开始注册
			Intent registerIntent = new Intent(this, RegisterActivity.class);
			startActivity(registerIntent);
			break;

		default:
			break;
		}
	}
}
